package battleGame;
import java.util.*;
public class World 
{
	int numRows;
	int numCols;
	
        // tenemos un maze para cada equipo que contiene las posiciones de los soldados y de los obstaculos
        //para el ejercito 1

	private Tile[][] worldMatrix;       
	GameLogic gameLogic;
	WarFlag flag;
	
	static private World me = null;
	static public World GetInstance()
	{
		if( me == null )
			System.out.print( "World instance is null \n" );
		return me;
	}
	
	static public World BuildWorld( int r, int c )
	{
		System.out.print( "El mundo esta siendo construido\n" );
		if( me == null )
		{
			me = new World( r, c );
		}
		return me;
	}
	
	private World(int r, int c)
	{
		numRows = r;
		numCols = c;
		worldMatrix = new Tile[numRows][numCols];
		    
		 //create tiles
		 int tileID = 0;
		 System.out.println("Rows:"+numRows);
		 System.out.println("Cols:"+numCols);
		 for(int i=0;i<numRows; i++){
			 
			 for(int j=0; j<numCols; j++)
			 {
				 String id = i+""+","+j;
				 Tile tile = new Tile(tileID,"",true);
				 tileID++;
				 worldMatrix[i][j] = tile;
		                  
			 }
			 
		 }
		 
		 Time.GetInstance();
		
		 //setup world, inaccesible places
		 fillTile(3,23);
		 fillTile(3,24);
		 fillTile(4,24);
		 fillTile(4,15);
		 fillTile(4,16);
		 fillTile(5,12);
		 fillTile(5,13);
		 fillTile(5,14);
		 fillTile(5,15);
		 fillTile(6,11);
		 fillTile(6,12);
		 fillTile(7,2);
		 fillTile(7,5);
		 fillTile(7,6);
		 fillTile(7,12);
		 //fillTile(8,0);
		 fillTile(8,1);
		 fillTile(8,2);
		 fillTile(8,3);
		 fillTile(8,4);
		 fillTile(8,5);
		 fillTile(8,6);
		 fillTile(8,7);
		 fillTile(8,8);
		 //fillTile(8,9);
		 fillTile(8,10);
		 fillTile(8,11);
		 fillTile(8,12);
		 //fillTile(8,0);
		 fillTile(9,16);
		 fillTile(9,17);
		 fillTile(10,18);
		 fillTile(11,19);
		 fillTile(12,23);
		 fillTile(12,24);
		 
		 gameLogic = new GameLogic(this);
		 flag = new WarFlag( );
		 Tile tile;
		 while( ( tile = this.GetTile( flag.position.getGridCol(), flag.position.getGridRow() ) ) == null || !tile.isEmpty )
		 {
			 flag.Init( Vector2.GetRandomPoint() );
		 }
		 //fillTile( flag.position.getGridRow(), flag.position.getGridCol() );
	}
	
	public void setStart(Tile s)
	{
		s.label="START";
	}
	public void setGoal(Tile g)
	{
		g.label="GOAL";
	}
	
	public AStarNode startSearch(Tile s, Tile g){
		//setStart(s);
		setGoal(g);
		AStarSearch mySearch = new AStarSearch(this);
		List<AStarNode> path;
		
		path=mySearch.findPath(s, g);
		if(path!=null)
		{
			Iterator it = path.iterator();
			/*
			while(it.hasNext()){
				AStarNode node = (AStarNode)it.next();
				System.out.println("Path is "+node.id);
				
			}
			*/
		}
        return path.get(0); //return the first occurence of the list
	}
	
/*	public void test1(){
		System.out.println("Start a search");
		AStarNode firstNode =startSearch(worldMatrix[14][1],worldMatrix[12][9]);
		double[] newRowsCols= new double[2];
		newRowsCols=getCoordsFromID(firstNode.id);
		platoon1[1].newCol=newRowsCols[1];
		platoon1[1].newRow=newRowsCols[0];
		platoon1[1].currentAction = Warrior.ACTION_MOVE;
		
		
	}
*/
	public void fillTile(int r, int c)
	{
	
		worldMatrix[r][c].isEmpty = false;
		worldMatrix[r][c].label = "X";
                
               
	}
	public void emptyTile(int r, int c)
	{
		worldMatrix[r][c].isEmpty = true;
		worldMatrix[r][c].label = "";
		
	}
	/*updates all characters position in the map 
	 * */
	 
	public void updateCharacters()
	{		
		for( int i = 0; i< gameLogic.army1WarriorList.size(); i++)
		{
			gameLogic.army1WarriorList.elementAt( i ).animate();
		}
		for( int i = 0; i < gameLogic.army2WarriorList.size(); i++ )
		{
			gameLogic.army2WarriorList.elementAt( i ).animate();
		}		
	}
	
	public AStarNode getNodeFromID(int id)
	  {
		  Tile tile = new Tile(-1,"ERROR",false);
		  for(int i = 0; i<numRows;i++){
			  
			  for(int j=0; j<numCols;j++)
			  {
				  if(worldMatrix[i][j].id==id)
					  tile = worldMatrix[i][j];
			  }
		  }
		  return tile;
	  }
	 
	 public int[] getCoordsFromID(int id)
	  {
		  int[] coords = new int[2];
		  for(int i = 0; i<numRows;i++){
			  
			  for(int j=0; j<numCols;j++)
			  {
				  if(worldMatrix[i][j].id==id)
				  {
					coords[0]=i; coords[1]=j;  
				  }
					
			  }
		  }
		  return coords;
		  
	  }

	public void updateWorld()
	{
		//all the game info is updated here
		Time.updateFrame();
		//updateTiles();
		updateCharacters();
		gameLogic.update();
	}
	
	public void updateTiles()
	{
		//check where the characters are standing 
		for( int i = 0; i < gameLogic.army1WarriorList.size(); i++ )
		{
			 fillTile( gameLogic.army1WarriorList.elementAt( i ).position.getGridRow(),
					   gameLogic.army1WarriorList.elementAt( i ).position.getGridCol() );
		}
		for( int i = 0; i < gameLogic.army2WarriorList.size(); i++ )
		{
			 fillTile( gameLogic.army2WarriorList.elementAt( i ).position.getGridRow(), 
					   gameLogic.army2WarriorList.elementAt( i ).position.getGridCol() );			 
		 }	
	}
	
	public Tile GetTile( int col, int row )
	{
		if( col >= numCols || row >= numRows || col < 0 || row < 0 )
		{
//			System.out.print( "GetTile parameters were outside bounds" );
			return null;
		}
			
		return worldMatrix[ row ][ col ];
	}
}

